<template>
  <div>
    <h1>新增配送员</h1>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="150px"
      class="demo-ruleForm"
    >
      <el-form-item label="配送员姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="配送员电话" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="配送员地址" prop="address">
        <el-input v-model="ruleForm.address"></el-input>
      </el-form-item>
      <el-form-item label="配送员出生日期">
        <el-date-picker
          v-model="ruleForm.date"
          type="date"
          placeholder="选择日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所属店铺ID" prop="storeId">
        <el-select
          v-model="ruleForm.storeId"
          placeholder="请选择店铺ID"
          @focus="getstores()"
        >
          <el-option
            v-for="item in storesrows"
            :key="item._id"
            :title="item._id"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即新增</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapActions, mapState } = createNamespacedHelpers("distribution");
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        phone: "",
        address: "",
        date: "",
        storeId: "6123aa9110807351b0dd0dd4",
        busy: true,
      },
      rules: {
        name: [
          { required: true, message: "请输入配送员姓名", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入配送员电话", trigger: "blur" },
        ],
        address: [
          { required: true, message: "请输入配送员地址", trigger: "blur" },
        ],
        date: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const newdistribution = this.ruleForm;
          this.addDistribution(newdistribution);
          alert("新增成功！");
          this.$router.push({
            path: `/info/distribution`,
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    ...mapActions(["getstores", "addDistribution"]),
  },
  computed: {
    ...mapState(["storesrows"]),
  },
};
</script>
